package com.pkk.geer.rose.web.controller;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import com.pkk.geer.rose.common.req.ConstellationLuckReqData;
import com.pkk.geer.rose.entity.RoseSpeedAppEntity;
import com.pkk.geer.rose.service.professional.ConstellationAppService;
import com.pkk.geer.rose.service.professional.RoseSpeedAppService;
import com.pkk.penguin.util.image.support.dto.ImageTextAttri;
import com.pkk.penguin.util.image.support.enums.ColorTypeEnum;
import com.pkk.penguin.util.image.support.factory.PenguinImageFactory;
import com.pkk.support.cache.client.CacheClient;
import com.pkk.support.penguin.business.log.annotation.BusinessLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.awt.Font;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 星座小程序的控制器
 * @author: peikunkun
 * @create: 2019-11-07 16:40
 **/
@Slf4j
@RestController
@RequestMapping("/constellation")
@Api(tags = "星座运势")
public class ConstellationAppController {


  @Resource
  private CacheClient cacheClient;
  @Resource
  private StringRedisTemplate stringRedisTemplate;
  @Resource
  private RoseSpeedAppService roseSpeedAppService;
  @Resource
  private ConstellationAppService constellationAppService;


  /**
   * @Description: 星座运势查询
   * @Param: []
   * @Author: peikunkun
   * @Date: 2019/11/9 0009 下午 1:54
   */
  @BusinessLog(desc = "星座运势查询")
  @RequestMapping(value = "/lucks")
  @ApiOperation(value = "星座运势查询", httpMethod = "POST")
  public CommonResponse<JSONObject> lucks(@RequestBody(required = false) ConstellationLuckReqData reqData) {
    JSONObject lucks = constellationAppService.handleConstellationLucks(reqData);
    return ResponseUtil.success(lucks);
  }


  @BusinessLog(desc = "星座运势处理")
  @RequestMapping(value = "/hlucks")
  @ApiOperation(value = "星座运势查询", httpMethod = "POST")
  public CommonResponse<JSONObject> hlucks(@RequestBody(required = false) ConstellationLuckReqData reqData)
      throws FileNotFoundException {
    String key = "hlucks";
    final String jsonByRedis = cacheClient.get(key);
    if (StringUtils.isNotBlank(jsonByRedis)) {
      return ResponseUtil.success(JSONObject.parseObject(jsonByRedis));
    }

    JSONObject lucks = constellationAppService.handleConstellationLucks(reqData);
    final String presummary = "今日运势:".concat(lucks.getString("presummary"));
    //处理日期
    final String date = "运势日期:".concat(lucks.getString("date"));
    //贵人星座
    final String star = "贵人星座:".concat(lucks.getString("star"));
    //幸运颜色
    final String color = "幸运颜色:".concat(lucks.getString("color"));
    //幸运数字
    final String number = "幸运数字:".concat(lucks.getString("number"));
    //综合运势
    final String summary = "综合运势:".concat(lucks.getString("summary"));
    //财运运势
    final String money = "财运运势:".concat(lucks.getString("money"));
    //工作运势
    final String career = "工作运势:".concat(lucks.getString("career"));
    //爱情运势
    final String love = "爱情运势:".concat(lucks.getString("love"));
    //健康运势
    final String health = "健康运势:".concat(lucks.getString("health"));

    List<ImageTextAttri> imageTextAttris = new ArrayList<>();
    final Font defaultFont = new Font("楷体", Font.BOLD, 50);
    imageTextAttris.add(ImageTextAttri.builder().top(1300f).left(50f).width(1000).content(presummary).font(defaultFont)
        .lineHeight(50).lineIndent(true).trim(false).lineWordNum(16).contentColor(ColorTypeEnum.CHOCOLATE).build());
    imageTextAttris
        .add(ImageTextAttri.builder().top(1600f).left(50f).width(1000).content(date).font(defaultFont).lineHeight(40)
            .lineIndent(true).trim(false).lineWordNum(16).contentColor(ColorTypeEnum.CHOCOLATE).build());
    imageTextAttris
        .add(ImageTextAttri.builder().top(1650f).left(50f).width(1000).content(star).font(defaultFont).lineHeight(40)
            .lineIndent(true).trim(false).lineWordNum(16).contentColor(ColorTypeEnum.CHOCOLATE).build());
    imageTextAttris
        .add(ImageTextAttri.builder().top(1700f).left(50f).width(1000).content(color).font(defaultFont).lineHeight(40)
            .lineIndent(true).trim(false).lineWordNum(16).contentColor(ColorTypeEnum.CHOCOLATE).build());
    imageTextAttris
        .add(ImageTextAttri.builder().top(1750f).left(50f).width(1000).content(number).font(defaultFont).lineHeight(40)
            .lineIndent(true).trim(false).lineWordNum(16).contentColor(ColorTypeEnum.CHOCOLATE).build());
    imageTextAttris
        .add(ImageTextAttri.builder().top(1800f).left(50f).width(1000).content(summary).font(defaultFont).lineHeight(40)
            .lineIndent(true).trim(false).lineWordNum(16).contentColor(ColorTypeEnum.CHOCOLATE).build());
    imageTextAttris
        .add(ImageTextAttri.builder().top(1850f).left(50f).width(1000).content(money).font(defaultFont).lineHeight(40)
            .lineIndent(true).trim(false).lineWordNum(16).contentColor(ColorTypeEnum.CHOCOLATE).build());
    imageTextAttris
        .add(ImageTextAttri.builder().top(1900f).left(50f).width(1000).content(career).font(defaultFont).lineHeight(40)
            .lineIndent(true).trim(false).lineWordNum(16).contentColor(ColorTypeEnum.CHOCOLATE).build());
    imageTextAttris
        .add(ImageTextAttri.builder().top(1950f).left(50f).width(1000).content(love).font(defaultFont).lineHeight(40)
            .lineIndent(true).trim(false).lineWordNum(16).contentColor(ColorTypeEnum.CHOCOLATE).build());
    imageTextAttris
        .add(ImageTextAttri.builder().top(2000f).left(50f).width(1000).content(health).font(defaultFont).lineHeight(40)
            .lineIndent(true).trim(false).lineWordNum(16).contentColor(ColorTypeEnum.CHOCOLATE).build());

    final String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
    final ArrayList<FileInputStream> execPath = new ArrayList<>();
    execPath.add(new FileInputStream(path.concat("statics/img/1.jpg")));
    execPath.add(new FileInputStream(path.concat("statics/img/2.jpg")));
    execPath.add(new FileInputStream(path.concat("statics/img/3.jpg")));
    execPath.add(new FileInputStream(path.concat("statics/img/4.jpg")));
    execPath.add(new FileInputStream(path.concat("statics/img/5.jpg")));
    execPath.add(new FileInputStream(path.concat("statics/img/6.jpg")));
    execPath.add(new FileInputStream(path.concat("statics/img/7.jpg")));
    final JSONArray jsonArray = new JSONArray();

    for (int i = 0; i < execPath.size(); i++) {
      String filePath = PenguinImageFactory.getInstant().makeImageTextToOss(execPath.get(i), imageTextAttris);
      FileUtil.appendUtf8String(filePath + "\n", "D:\\log\\saveFileToOss.log");
      jsonArray.add(filePath);
    }

    final JSONObject finalJson = new JSONObject();
    finalJson.put("today", jsonArray);
    cacheClient.setex(key, 60 * 60 * 3, finalJson.toString());
    return ResponseUtil.success(finalJson);
  }


  /**
   * @Description:星座运势查询
   * @Param: []
   * @return: com.pkk.components.rpc.response.CommonResponse
   * @Author: peikunkun
   * @Date: 2019/11/7 0007 下午 4:44
   */
  @BusinessLog(desc = "星座运势查询")
  @RequestMapping(value = "/luck")
  @ApiOperation(value = "星座运势查询", httpMethod = "POST")
  public CommonResponse luck() {
    return ResponseUtil.success("success");
  }


  /**
   * @Description: 测试
   * @Param: [appFlag]
   * @Author: peikunkun
   * @Date: 2019/11/8 0008 下午 6:03
   */
  @BusinessLog(desc = "test")
  @RequestMapping(value = "/test")
  @ApiOperation(value = "test", httpMethod = "POST")
  public CommonResponse<List<RoseSpeedAppEntity>> test(String appFlag) {
    return ResponseUtil.success(roseSpeedAppService.getRoseSpeedAppByAppFlag(appFlag));
  }
}
